package org.flower.web.servlet;

import com.alibaba.fastjson.JSON;
import org.flower.dao.AddressDao;
import org.flower.dao.impl.AddressDaoImpl;
import org.flower.entity.Address;
import org.flower.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/user/address.shtml")
public class addressServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        AddressDao adao = new AddressDaoImpl();
        User u = (User)request.getSession().getAttribute("user");
        //获取进来做什么的标志
        String flag = request.getParameter("flag");
        switch(flag){
            case "insertAddress":
                //添加新地址
                //接收参数 封装对象 执行添加
                String name = request.getParameter("name");
                String phone = request.getParameter("phone");
                String addressDetails = request.getParameter("addressDetails");
                String province = request.getParameter("j1");
                String city = request.getParameter("j2");
                String area = request.getParameter("j3");
                Address a = new Address();
                a.setUserId(u.getId());
                a.setPhone(phone);
                a.setAddressContent(addressDetails);
                a.setShouName(name);
                a.setProvince(Integer.parseInt(province));
                a.setCity(Integer.parseInt(city));
                a.setArea(Integer.parseInt(area));
                boolean isok = adao.addAddress(a);
                //如果执行成功 返还一个地址对象
                if(isok){
                   int aid = adao.getMaxIdByUid(u.getId());
                   Address newA = adao.getAddressById(aid);//新地址
                   out.print(JSON.toJSONString(newA));
                }else{
                   out.print("");
                }
                break;
            case "selectAllAddress":
                //查询所有用户的收获地址
                List<Address> allAddress = adao.getAllAddressByUid(u.getId());
                out.print(JSON.toJSONString(allAddress));
                break;
            case "setdefault":
                //设置为默认地址
                String id = request.getParameter("id");
                boolean setdef = adao.updateSetDefaultById(Integer.parseInt(id));
                if(setdef){
                    //设置成功 将不是该条编号的 并且是默认地址的 取消默认地址
                    List<Integer> aidList = adao.getAllChuDefaultQiTaAddressId(Integer.parseInt(id),u.getId());
                    if(aidList.size()>0){
                        for(Integer addressId : aidList){
                            adao.updateUnbindDefault(addressId);
                        }
                    }
                }
                out.print(setdef);
                break;
            case "deleteAddress":
                //删除地址信息
                //获取地址编号
                String iddel = request.getParameter("id");
                //执行删除、
                boolean delisok = adao.deleteAddressById(Integer.parseInt(iddel));
                out.print(delisok);
                break;
            case "selectById":
                //查询单个地址 并返回
                //获取地址编号
                String modifyId = request.getParameter("id");
                Address aOne = adao.getAddressById(Integer.parseInt(modifyId));
                out.print(JSON.toJSONString(aOne));
                break;
            case "updateAddress":
                //修改地址信息
                //获取地址编号
                String updateId = request.getParameter("id");
                String name2 = request.getParameter("name");
                String phone2 = request.getParameter("phone");
                String addressDetails2 = request.getParameter("addressDetails");
                String province2 = request.getParameter("j1");
                String city2 = request.getParameter("j2");
                String area2 = request.getParameter("j3");
                //封装对象
                Address a2 = new Address();
                a2.setId(Integer.parseInt(updateId));
                a2.setShouName(name2);
                a2.setPhone(phone2);
                a2.setAddressContent(addressDetails2);
                a2.setProvince(Integer.parseInt(province2));
                a2.setCity(Integer.parseInt(city2));
                a2.setArea(Integer.parseInt(area2));
                //执行修改
                adao.updateAddress(a2);
                out.print(JSON.toJSONString(adao.getAddressById(a2.getId())));
                break;
            case "selectAddressCount":
                //查询地址总条数
                out.print(adao.getAddressCountByUid(u.getId()));
                break;
            default:
                out.print(false);
                break;
        }
    }
}
